import dayjs from '@/plugins/dayjs'
import { RecordProgram } from '@/api/meetingSpace'
export interface DownloadJson {
	name: string
	proList: {
		indexList: { hlsIndex: string }[]
	}[]
}
export interface ICardDetail {
	id: number
	name: string
	asfPath: string
}
export interface ICard {
	programId: number
	programName: string
	programEncMode: 1 | 2 | 4
	createTime: string
	duration: string
	jsonPath: string
	snapshotPath: string
	details: ICardDetail[]
	streamjsonpath: string
}
function padTime(time: number): string {
	return time < 10 ? `0${time}` : `${time}`
}
function parseDuration(time: number) {
	const hours = Math.floor(time / 60 / 60)
	const minutes = Math.floor((time - hours * 60 * 60) / 60)
	const seconds = time - hours * 60 * 60 - minutes * 60
	return `${padTime(hours)}:${padTime(minutes)}:${padTime(seconds)}`
}
export function transHttpUrl(url: string) {
	let _url = url
	if (!(_url.startsWith('http://') || _url.startsWith('https://'))) {
		_url = `${window.location.protocol}//${_url}`
	}
	if (_url.endsWith('/')) {
		_url = _url.slice(0, _url.length - 1)
	}
	return _url
}
export function translatorCard(
	item: RecordProgram,
	{ token, vrsAddress }: { token: string; vrsAddress: string }
): ICard {
	const createTime = dayjs.unix(item.createtime).format('YYYY-MM-DD HH:mm')
	const duration = parseDuration(item.prgallduration)
	const coverPath = item.prgdetail?.[0]?.['coverpath']
	const coverName = item.prgdetail?.[0]?.['covername'] ?? ''
	const details = item.prgdetail?.map((x) => ({
		id: item.prggid,
		name: x.prgname,
		asfPath: `${vrsAddress}${x.relativepath}${encodeURIComponent(x.prgname)}`,
	}))
	return {
		programId: item.prggid,
		programName: item.prgalias,
		programEncMode: item.prgencmode,
		createTime,
		duration,
		jsonPath: item.streamjsonpath ? `${vrsAddress}${item.streamjsonpath}?sso_token=${token}&download=1` : '',
		snapshotPath: coverPath ? `${vrsAddress}${coverPath}${coverName}` : '',
		details,
		streamjsonpath: item.streamjsonpath,
	}
}
